﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Admin2025.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class Init2 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "AppPermissionAppRole");

            migrationBuilder.DropTable(
                name: "AppRoleAppUser");

            migrationBuilder.RenameColumn(
                name: "Name",
                table: "AppPermission",
                newName: "PermissionName");

            migrationBuilder.AddColumn<string>(
                name: "Code",
                table: "AppUser",
                type: "text",
                nullable: true);

            migrationBuilder.AddColumn<Guid>(
                name: "DepartmentId",
                table: "AppUser",
                type: "uuid",
                nullable: true);

            migrationBuilder.AddColumn<Guid>(
                name: "AppUserId",
                table: "AppRole",
                type: "uuid",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Code",
                table: "AppRole",
                type: "text",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "DataScope",
                table: "AppRole",
                type: "text",
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<Guid>(
                name: "AppRoleId",
                table: "AppPermission",
                type: "uuid",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Code",
                table: "AppPermission",
                type: "text",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "AppDepartment",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    DepartmentName = table.Column<string>(type: "text", nullable: false),
                    ParentId = table.Column<Guid>(type: "uuid", nullable: true),
                    Code = table.Column<string>(type: "text", nullable: true),
                    IsActived = table.Column<bool>(type: "boolean", nullable: false),
                    IsDeleted = table.Column<bool>(type: "boolean", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    CreatedBy = table.Column<Guid>(type: "uuid", nullable: false),
                    UpdatedBy = table.Column<Guid>(type: "uuid", nullable: false),
                    DisplayOrder = table.Column<int>(type: "integer", nullable: false),
                    Remarks = table.Column<string>(type: "text", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AppDepartment", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "AppResource",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    ResourceName = table.Column<string>(type: "text", nullable: false),
                    ResourceType = table.Column<string>(type: "text", nullable: false),
                    Path = table.Column<string>(type: "text", nullable: false),
                    AppPermissionId = table.Column<Guid>(type: "uuid", nullable: true),
                    Code = table.Column<string>(type: "text", nullable: true),
                    IsActived = table.Column<bool>(type: "boolean", nullable: false),
                    IsDeleted = table.Column<bool>(type: "boolean", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    CreatedBy = table.Column<Guid>(type: "uuid", nullable: false),
                    UpdatedBy = table.Column<Guid>(type: "uuid", nullable: false),
                    DisplayOrder = table.Column<int>(type: "integer", nullable: false),
                    Remarks = table.Column<string>(type: "text", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AppResource", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AppResource_AppPermission_AppPermissionId",
                        column: x => x.AppPermissionId,
                        principalTable: "AppPermission",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_AppUser_DepartmentId",
                table: "AppUser",
                column: "DepartmentId");

            migrationBuilder.CreateIndex(
                name: "IX_AppRole_AppUserId",
                table: "AppRole",
                column: "AppUserId");

            migrationBuilder.CreateIndex(
                name: "IX_AppPermission_AppRoleId",
                table: "AppPermission",
                column: "AppRoleId");

            migrationBuilder.CreateIndex(
                name: "IX_AppResource_AppPermissionId",
                table: "AppResource",
                column: "AppPermissionId");

            migrationBuilder.AddForeignKey(
                name: "FK_AppPermission_AppRole_AppRoleId",
                table: "AppPermission",
                column: "AppRoleId",
                principalTable: "AppRole",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_AppRole_AppUser_AppUserId",
                table: "AppRole",
                column: "AppUserId",
                principalTable: "AppUser",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_AppUser_AppDepartment_DepartmentId",
                table: "AppUser",
                column: "DepartmentId",
                principalTable: "AppDepartment",
                principalColumn: "Id");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_AppPermission_AppRole_AppRoleId",
                table: "AppPermission");

            migrationBuilder.DropForeignKey(
                name: "FK_AppRole_AppUser_AppUserId",
                table: "AppRole");

            migrationBuilder.DropForeignKey(
                name: "FK_AppUser_AppDepartment_DepartmentId",
                table: "AppUser");

            migrationBuilder.DropTable(
                name: "AppDepartment");

            migrationBuilder.DropTable(
                name: "AppResource");

            migrationBuilder.DropIndex(
                name: "IX_AppUser_DepartmentId",
                table: "AppUser");

            migrationBuilder.DropIndex(
                name: "IX_AppRole_AppUserId",
                table: "AppRole");

            migrationBuilder.DropIndex(
                name: "IX_AppPermission_AppRoleId",
                table: "AppPermission");

            migrationBuilder.DropColumn(
                name: "Code",
                table: "AppUser");

            migrationBuilder.DropColumn(
                name: "DepartmentId",
                table: "AppUser");

            migrationBuilder.DropColumn(
                name: "AppUserId",
                table: "AppRole");

            migrationBuilder.DropColumn(
                name: "Code",
                table: "AppRole");

            migrationBuilder.DropColumn(
                name: "DataScope",
                table: "AppRole");

            migrationBuilder.DropColumn(
                name: "AppRoleId",
                table: "AppPermission");

            migrationBuilder.DropColumn(
                name: "Code",
                table: "AppPermission");

            migrationBuilder.RenameColumn(
                name: "PermissionName",
                table: "AppPermission",
                newName: "Name");

            migrationBuilder.CreateTable(
                name: "AppPermissionAppRole",
                columns: table => new
                {
                    PermissionsId = table.Column<Guid>(type: "uuid", nullable: false),
                    RolesId = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AppPermissionAppRole", x => new { x.PermissionsId, x.RolesId });
                    table.ForeignKey(
                        name: "FK_AppPermissionAppRole_AppPermission_PermissionsId",
                        column: x => x.PermissionsId,
                        principalTable: "AppPermission",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_AppPermissionAppRole_AppRole_RolesId",
                        column: x => x.RolesId,
                        principalTable: "AppRole",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "AppRoleAppUser",
                columns: table => new
                {
                    RolesId = table.Column<Guid>(type: "uuid", nullable: false),
                    UsersId = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AppRoleAppUser", x => new { x.RolesId, x.UsersId });
                    table.ForeignKey(
                        name: "FK_AppRoleAppUser_AppRole_RolesId",
                        column: x => x.RolesId,
                        principalTable: "AppRole",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_AppRoleAppUser_AppUser_UsersId",
                        column: x => x.UsersId,
                        principalTable: "AppUser",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_AppPermissionAppRole_RolesId",
                table: "AppPermissionAppRole",
                column: "RolesId");

            migrationBuilder.CreateIndex(
                name: "IX_AppRoleAppUser_UsersId",
                table: "AppRoleAppUser",
                column: "UsersId");
        }
    }
}
